package com.kylin.module.shop.actions;


import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.kylin.common.Constants;
import com.kylin.common.dict.orm.Dict;
import com.kylin.common.entity.MsgKey;
import com.kylin.common.exception.BusinessException;
import com.kylin.common.paginate.DisplayPage;
import com.kylin.common.paginate.PageManager;
import com.kylin.hibernate.BaseHibernateDAO;
import com.kylin.hibernate.CriteriaInfo;
import com.kylin.hibernate.PropertyCondition;
import com.kylin.module.shop.business.CompanyBlogic;
import com.kylin.module.shop.forms.ProductForm;
import com.kylin.module.shop.orm.Advisory;
import com.kylin.module.shop.orm.Content;
import com.kylin.module.shop.orm.Order;
import com.kylin.module.shop.orm.Product;
import com.kylin.struts.BaseActionInterface;
import com.kylin.struts.BaseDispatchAction;

public class ManageProductAction extends BaseDispatchAction  {

	protected static Logger log = Logger.getLogger(ManageProductAction.class);

	public ActionForward add(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.info("添加商品 : "+request.getParameter("name"));
		String forward = "addProductResult";
		CompanyBlogic blogic = new CompanyBlogic();
		try {
			ProductForm form = (ProductForm)actionForm;
			blogic.addProduct(form);
			log.debug("添加商品成功" + form.getName());
			// 返回成功的信息，需要在结果页面添加 <mytag:MyActionMsg />
	        request.setAttribute(Constants.MSGKEY, new MsgKey("","添加商品成功"));
		}catch (Exception e) {
			forward = doException( request, e, "addProductResult");
		}
		return mapping.findForward(forward);
	}

	public ActionForward bfUpdate(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("查看修改产品 : "+request.getParameter("id"));
		String forward = "bfUpdate";
		CompanyBlogic blogic = new CompanyBlogic();
		try {
			Product p = blogic.getProduct(request.getParameter("id"));
			request.setAttribute(Constants.KEY_REQUEST_RESULT,p);
		}catch (Exception e) {
			forward = doException( request, e, forward);
		}
		return mapping.findForward(forward);
	}

	public ActionForward delete(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.info("删除商品 : "+request.getParameter("id"));
		int pageSize = 10;// 分页列表的每页行数  
		String forward = "listProducts";
		CompanyBlogic blogic = new CompanyBlogic();
		try {
			blogic.deleteProduct(new Long(request.getParameter("id")));
			log.debug("删除商品成功ok" );
			// 返回成功的显示信息，需要在结果页面添加 <mytag:MyActionMsg />
	        request.setAttribute(Constants.MSGKEY, new MsgKey("删除商品成功ok","删除商品成功"));
		}catch (Exception e) {
			forward = doException( request, e, forward);
		}
		try {
	        CriteriaInfo cinfo = getUserCacheCriteriaInfo(request, request.getParameter("searchId"));
			DisplayPage page = blogic.getProductList(pageSize,Integer.parseInt(request.getParameter("page")), cinfo);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, page);        
		} catch (BusinessException e) {
		}
		return mapping.findForward(forward);
	}

	/**
	 * 将产品加入到展示区
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward toShowProducts(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		int pageSize = 10;// 分页列表的每页行数  
		String[] ids = request.getParameterValues("ids");
		if(ids == null)
			ids = new String[]{request.getParameter("ids")};
		log.info("展示商品 : length="+ ids.length);
		String forward = "listProducts";
		CompanyBlogic blogic = new CompanyBlogic();
		try {
			blogic.toShowProducts(ids,new Long(request.getParameter("companyId")));
			log.debug("已将选定商品加到展示区" );
			// 返回成功的显示信息，需要在结果页面添加 <mytag:MyActionMsg />
	        request.setAttribute(Constants.MSGKEY, new MsgKey("","已将选定商品加到展示区"));
		}catch (Exception e) {
			forward = doException( request, e, forward);
		}
		try {
	        CriteriaInfo cinfo = getUserCacheCriteriaInfo(request, request.getParameter("searchId"));
			DisplayPage page = blogic.getProductList(pageSize,Integer.parseInt(request.getParameter("page")), cinfo);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, page);        
		} catch (BusinessException e) {
		}
		return mapping.findForward(forward);
	}

	/**
	 * 将产品加入到展示区
	 * @param mapping
	 * @param actionForm
	 * @param request
	 * @param response
	 * @return
	 */
	public ActionForward toProductsDB(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		int pageSize = 10;// 分页列表的每页行数  
		String[] ids = request.getParameterValues("ids");
		if(ids == null)
			ids = new String[]{request.getParameter("ids")};
		log.info("将产品入库 : length="+ ids.length);
		String forward = "listProducts";
		CompanyBlogic blogic = new CompanyBlogic();
		try {
			blogic.toProductsDB(ids);
			log.debug("已将选定产品入库" );
			// 返回成功的显示信息，需要在结果页面添加 <mytag:MyActionMsg />
	        request.setAttribute(Constants.MSGKEY, new MsgKey("","已将选定产品入库"));
	        CriteriaInfo cinfo = getUserCacheCriteriaInfo(request, request.getParameter("searchId"));
			DisplayPage page = blogic.getProductList(pageSize,Integer.parseInt(request.getParameter("page")), cinfo);
			request.setAttribute(Constants.KEY_REQUEST_RESULT, page);        
		}catch (Exception e) {
			forward = doException( request, e, forward);
		}
		return mapping.findForward(forward);
	}

	public ActionForward update(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.info("修改商品 : "+request.getParameter("name"));
		String forward = "update";
		CompanyBlogic blogic = new CompanyBlogic();
		try {
			ProductForm form = (ProductForm)actionForm;
			Product p = blogic.updateProduct(form);
			request.setAttribute(Constants.KEY_REQUEST_RESULT,p);
			log.debug("修改商品成功" + form.getName());
			// 返回成功的弹出信息，需要在结果页面添加 <mytag:MyActionMsg />
	        request.setAttribute(Constants.MSGKEY, new MsgKey("","修改商品成功"));
		}catch (Exception e) {
			doException( request, e, "update");
			return bfUpdate( mapping,  actionForm,  request,  response);
		}
		return mapping.findForward(forward);
	}

	public ActionForward getOrderDetail(ActionMapping mapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) {
		log.debug("查看订单详情 : "+request.getParameter("id"));
		String forward = "orderDetail";
		CompanyBlogic blogic = new CompanyBlogic();
		try {
			Order obj = blogic.getOrder(request.getParameter("id"));
			request.setAttribute(Constants.KEY_REQUEST_RESULT,obj);
		}catch (Exception e) {
			forward = doException( request, e, forward);
		}
		return mapping.findForward(forward);
	}

}
